﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class ReviewDAL
    {
        #region Add
        public int Add(ReviewInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ReviewID",SqlDbType.Int,4),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID)),
                db.MakeInParam("@ActivityID",SqlDbType.Int,4,info.ActivityID),
                db.MakeInParam("@TableNumber",SqlDbType.VarChar,20,info.TableNumber),
                db.MakeInParam("@CreateDate",SqlDbType.DateTime,8,info.CreateDate),
                db.MakeInParam("@TagUID",SqlDbType.VarChar,50,info.TagUID),
                db.MakeInParam("@BrowserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.BrowserGUID))
            };
            try
            {
                db.ExecNonQuery("T2R_Review_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get the Review by BrowserGUID
        public DataTable GetByBrowserGUID(string browserguid)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@BrowserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(browserguid))
                };
                return db.GetDataSet("T2R_Review_GetByBrowserGUID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get By ID
        public ReviewInfo GetByID(int listid)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@ReviewID",SqlDbType.Int,4,listid)
            };
            try
            {
                dr = db.RunProc("T2R_Review_GetByID", param);
                if (dr.Read())
                {
                    ReviewInfo info = new ReviewInfo();
                    info.ReviewID = DataHelper.GetInt(dr["ReviewID"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    info.ActivityID = DataHelper.GetInt(dr["ActivityID"]);
                    info.TableNumber = DataHelper.GetString(dr["TableNumber"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.TagUID = DataHelper.GetString(dr["TagUID"]);
                    info.BrowserGUID = DataHelper.GetString(dr["BrowserGUID"]);
                    
                    return info;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion
    }
}
